import { createRouter, createWebHistory } from 'vue-router';
import MainVue from '@/views/Main.vue';
const routes = [
    {
        path: '/',
        redirect: '/index',
    },
    {
        path: '/index',
        component: MainVue,
        children: [
            {
                path: '/home',
                name: "home",
                component: () => import("../views/home/home.vue")
            },
            {
                path: '/infoshow',
                name: "infoshow",
                component: () => import("../views/home/infoshow.vue")
            },
            {
                path: '/fundList',
                name: "fundList",
                component: () => import("../views/fundList/fundList.vue")
            },
        ]
    },
    {
        path: '/register',
        name: "register",
        component: () => import("../views/register/Register.vue")
    },
    {
        path: '/login',
        name: "login",
        component: () => import("../views/login/Login.vue")
    },
    {
          path: '/:pathMatch(.*)',
          name: 'notfound',
          component: () => import("../views/404.vue")
      }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});
router.beforeEach((to,from,next) =>{
    const isLogin = localStorage.mytoken ? true : false;
    if(to.path == '/login' || to.path == '/register'){
        next();
    } else{
        isLogin ? next() : next('/login');
    }
})

export default router;
